package cn.hnc.checkapi.service;

import cn.hnc.checkapi.bean.Axis;
import cn.hnc.checkapi.bean.MotorCheck;
import cn.hnc.checkapi.bean.NcVer;
import cn.hnc.checkapi.bean.Product;
import cn.hnc.checkapi.bean.Servo;
import cn.hnc.checkapi.bean.node.NodeResponse;
import cn.hnc.checkapi.repository.AxisRepository;
import cn.hnc.checkapi.repository.MotorCheckRepository;
import cn.hnc.checkapi.repository.NcVerRepository;
import cn.hnc.checkapi.repository.ServoRepository;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

@Service
public class NodeService {
   @Autowired
   RestTemplate restTemplate;
   @Autowired
   private AxisRepository axisRepository;
   @Autowired
   private MotorCheckRepository motorCheckRepository;
   @Autowired
   private ServoRepository servoRepository;
   @Autowired
   private NcVerRepository ncVerRepository;
   @Value("${node.url}")
   private String url;
   private String token;
   private Map allNameMap = this.dealDataName();

   public Map sendListRequest(List obj, String path) {
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
      requestHeaders.add("Authorization", "Bearer " + this.token);
      HttpEntity httpEntity = new HttpEntity(obj, requestHeaders);
      Map node = (Map)this.restTemplate.postForObject(this.url + path, httpEntity, Map.class, new Object[0]);
      if (node != null && "-1".equals(node.get("status").toString())) {
         this.token = this.loginToken();
         requestHeaders = new HttpHeaders();
         requestHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
         requestHeaders.add("Authorization", "Bearer " + this.token);
         httpEntity = new HttpEntity(obj, requestHeaders);
         node = (Map)this.restTemplate.postForObject(this.url + path, httpEntity, Map.class, new Object[0]);
      }

      return node;
   }

   public Map sendRequest(Map obj, String path) {
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
      requestHeaders.add("Authorization", "Bearer " + this.token);
      HttpEntity httpEntity = new HttpEntity(obj, requestHeaders);
      Map node = (Map)this.restTemplate.postForObject(this.url + path, httpEntity, Map.class, new Object[0]);
      if (node != null && "-1".equals(node.get("status").toString())) {
         this.token = this.loginToken();
         requestHeaders = new HttpHeaders();
         requestHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
         requestHeaders.add("Authorization", "Bearer " + this.token);
         httpEntity = new HttpEntity(obj, requestHeaders);
         node = (Map)this.restTemplate.postForObject(this.url + path, httpEntity, Map.class, new Object[0]);
      }

      return node;
   }

   public String loginToken() {
      HttpHeaders requestHeaders = new HttpHeaders();
      requestHeaders.setContentType(MediaType.parseMediaType("application/json;charset=UTF-8"));
      Map param = new HashMap();
      param.put("username", "HZSK20");
      param.put("password", "HZsk_890#");
      HttpEntity httpEntity = new HttpEntity(param, requestHeaders);
      NodeResponse node = (NodeResponse)this.restTemplate.postForObject(this.url + "/identity/token", httpEntity, NodeResponse.class, new Object[0]);
      StringBuilder token = new StringBuilder();
      if (node.getStatus() == 1) {
         token.append(node.getData().getToken());
      }

      return token.toString();
   }

   public Map dealDataName() {
      Map mapName = new HashMap();
      mapName.put("轴0", "axis0");
      mapName.put("轴1", "axis1");
      mapName.put("轴2", "axis2");
      mapName.put("轴3", "axis3");
      mapName.put("轴4", "axis4");
      mapName.put("轴5", "axis5");
      mapName.put("轴6", "axis6");
      mapName.put("轴7", "axis7");
      mapName.put("轴S", "axisS");
      mapName.put("accTime", "加速时间");
      mapName.put("accElecFF", "加速段电流峰峰值");
      mapName.put("accElecPulse", "加速段电流脉冲指标");
      mapName.put("accMaxFollowRate", "加速段最大跟随误差比");
      mapName.put("decTime", "减速时间");
      mapName.put("decElecFF", "减速段电流峰峰值");
      mapName.put("decElecPulse", "减速段电流脉冲指标");
      mapName.put("decMaxFollowRate", "减速段最大跟随误差比");
      mapName.put("constMaxFollowRate", "恒速段最大跟随误差比");
      mapName.put("constElecRMS", "恒速段电流均方根");
      mapName.put("constElecRMSMag", "恒速段电流均方根幅值");
      mapName.put("constElecVariance", "恒速段电流方差");
      mapName.put("constElecSkewness", "恒速段电流歪度");
      mapName.put("constElecKurtosis", "恒速段电流峭度");
      mapName.put("constElecWaveQuota", "恒速段电流波形指标");
      mapName.put("constElecMarginQuota", "恒速段电流裕度指标");
      List attributeList = new ArrayList();
      attributeList.add("加速时间");
      attributeList.add("加速段电流峰峰值");
      attributeList.add("加速段电流脉冲指标");
      attributeList.add("加速段最大跟随误差比");
      attributeList.add("减速时间");
      attributeList.add("减速段电流峰峰值");
      attributeList.add("减速段电流脉冲指标");
      attributeList.add("减速段最大跟随误差比");
      attributeList.add("恒速段最大跟随误差比");
      attributeList.add("恒速段电流均方根");
      attributeList.add("恒速段电流均方根幅值");
      attributeList.add("恒速段电流方差");
      attributeList.add("恒速段电流歪度");
      attributeList.add("恒速段电流峭度");
      attributeList.add("恒速段电流波形指标");
      attributeList.add("恒速段电流裕度指标");
      Map map = new LinkedHashMap();
      Map mainMap = new LinkedHashMap();
      map.put("电机数量", "macAxNum");
      map.put("是否成套检测", "partCheck");
      map.put("检测是否合格", "passFlag");
      map.put("成套系统出厂日期", "macProductTime");
      map.put("客户名", "customerName");
      map.put("合同号", "contractCode");
      map.put("记录上传时间", "uploadTime");
      mainMap.put("标识", "handle");
      mainMap.put("型号规格", "model");
      mainMap.put("出厂编号", "serialNo");
      mainMap.put("系统SN号", "systemSN");
      mainMap.put("物料编码", "matNo");
      mainMap.put("物料描述", "itemDescription");
      mainMap.put("数量", "count");
      mainMap.put("单位", "unit");
      mainMap.put("生产时间", "creatTime");
      mainMap.put("生产批次", "productionBatch");
      mainMap.put("订单号", "orderNumber");
      mainMap.put("制造商", "manufacturer");
      mainMap.put("供应商", "supplier");
      mainMap.put("电话", "telephone");
      mainMap.put("参数信息", "info");
      mainMap.put("溯源信息", "source");
      map.put("总线式I/O单元型号", "ioUnitType");
      map.put("总线式I/O单元编号", "ioUnitId");
      map.put("轴0", "axis0");
      map.put("轴1", "axis1");
      map.put("轴2", "axis2");
      map.put("轴3", "axis3");
      map.put("轴4", "axis4");
      map.put("轴5", "axis5");
      map.put("轴6", "axis6");
      map.put("轴7", "axis7");
      map.put("轴S", "axisS");
      map.put("系统软件版本标准值", "softVer");
      map.put("系统软件版本检测值", "testSoftVer");
      map.put("Ncu版本标准值", "ncuVer");
      map.put("Ncu版本检测值", "testNcuVer");
      map.put("PLC内核版本标准值", "plcVer");
      map.put("PLC内核版本检测值", "testPlcVer");
      map.put("DRV驱动程序版本标准值", "drvVer");
      map.put("DRV驱动程序版本检测值", "testDrvVer");
      map.put("参数标准值", "userParmVer");
      map.put("参数检测值", "testUserParmVer");
      map.put("用户PLC标准值", "userPlcVer");
      map.put("用户PLC检测值", "testUserPlcVer");
      map.put("OS标准值", "osVer");
      map.put("OS检测值", "testOsVer");
      map.put("Fpga标准值", "fpgaVer");
      map.put("Fpga检测值", "testFpgaVer");
      mapName.put("accTime", "加速时间");
      mapName.put("accElecFF", "加速段电流峰峰值");
      mapName.put("accElecPulse", "加速段电流脉冲指标");
      mapName.put("accMaxFollowRate", "加速段最大跟随误差比");
      mapName.put("decTime", "减速时间");
      mapName.put("decElecFF", "减速段电流峰峰值");
      mapName.put("decElecPulse", "减速段电流脉冲指标");
      mapName.put("decMaxFollowRate", "减速段最大跟随误差比");
      mapName.put("constMaxFollowRate", "恒速段最大跟随误差比");
      mapName.put("constElecRMS", "恒速段电流均方根");
      mapName.put("constElecRMSMag", "恒速段电流均方根幅值");
      mapName.put("constElecVariance", "恒速段电流方差");
      mapName.put("constElecSkewness", "恒速段电流歪度");
      mapName.put("constElecKurtosis", "恒速段电流峭度");
      mapName.put("constElecWaveQuota", "恒速段电流波形指标");
      mapName.put("constElecMarginQuota", "恒速段电流裕度指标");

      for(int j = 0; j < 3; ++j) {
         map.put("轴" + j + "伺服型号标准值", "axis" + j + "_servoType");
         map.put("轴" + j + "伺服型号检测值", "axis" + j + "_testServoType");
         map.put("轴" + j + "伺服版本标准值", "axis" + j + "_servoVer");
         map.put("轴" + j + "伺服版本检测值", "axis" + j + "_testServoVer");
         map.put("轴" + j + "加速时间", "axis" + j + "_accTime");
         map.put("轴" + j + "加速段电流峰峰值", "axis" + j + "_accElecFF");
         map.put("轴" + j + "加速段电流脉冲指标", "axis" + j + "_accElecPulse");
         map.put("轴" + j + "加速段最大跟随误差比", "axis" + j + "_accMaxFollowRate");
         map.put("轴" + j + "减速时间", "axis" + j + "_decTime");
         map.put("轴" + j + "减速段电流峰峰值", "axis" + j + "_decElecFF");
         map.put("轴" + j + "减速段电流脉冲指标", "axis" + j + "_decElecPulse");
         map.put("轴" + j + "减速段最大跟随误差比", "axis" + j + "_decMaxFollowRate");
         map.put("轴" + j + "恒速段最大跟随误差比", "axis" + j + "_constMaxFollowRate");
         map.put("轴" + j + "恒速段电流均方根", "axis" + j + "_constElecRMS");
         map.put("轴" + j + "恒速段电流均方根幅值", "axis" + j + "_constElecRMSMag");
         map.put("轴" + j + "恒速段电流方差", "axis" + j + "_constElecVariance");
         map.put("轴" + j + "恒速段电流歪度", "axis" + j + "_constElecSkewness");
         map.put("轴" + j + "恒速段电流峭度", "axis" + j + "_constElecKurtosis");
         map.put("轴" + j + "恒速段电流波形指标", "axis" + j + "_constElecWaveQuota");
         map.put("轴" + j + "恒速段电流裕度指标", "axis" + j + "_constElecMarginQuota");
      }

      Map allNameMap = new HashMap();
      allNameMap.put("main", mainMap);
      allNameMap.put("info", map);
      return allNameMap;
   }

   public Map dealData(Product product) throws JsonProcessingException {
      new HashMap();
      new HashMap();
      Map mainMap = (Map)this.allNameMap.get("main");
      Map infoMap = (Map)this.allNameMap.get("info");
      List servos = this.servoRepository.findByProductId(product.getId());
      List axisList = this.axisRepository.findByProductId(product.getId());
      List motorChecks = this.motorCheckRepository.findByProductId(product.getId());
      NcVer ncVer = this.ncVerRepository.findFirstByProductId(product.getId());
      Map mapName = new HashMap();
      mapName.put("轴0", "axis0");
      mapName.put("轴1", "axis1");
      mapName.put("轴2", "axis2");
      mapName.put("轴3", "axis3");
      mapName.put("轴4", "axis4");
      mapName.put("轴5", "axis5");
      mapName.put("轴6", "axis6");
      mapName.put("轴7", "axis7");
      mapName.put("轴S", "axisS");
      mapName.put("accTime", "加速时间");
      mapName.put("accElecFF", "加速段电流峰峰值");
      mapName.put("accElecPulse", "加速段电流脉冲指标");
      mapName.put("accMaxFollowRate", "加速段最大跟随误差比");
      mapName.put("decTime", "减速时间");
      mapName.put("decElecFF", "减速段电流峰峰值");
      mapName.put("decElecPulse", "减速段电流脉冲指标");
      mapName.put("decMaxFollowRate", "减速段最大跟随误差比");
      mapName.put("constMaxFollowRate", "恒速段最大跟随误差比");
      mapName.put("constElecRMS", "恒速段电流均方根");
      mapName.put("constElecRMSMag", "恒速段电流均方根幅值");
      mapName.put("constElecVariance", "恒速段电流方差");
      mapName.put("constElecSkewness", "恒速段电流歪度");
      mapName.put("constElecKurtosis", "恒速段电流峭度");
      mapName.put("constElecWaveQuota", "恒速段电流波形指标");
      mapName.put("constElecMarginQuota", "恒速段电流裕度指标");
      List attributeList = new ArrayList();
      attributeList.add("加速时间");
      attributeList.add("加速段电流峰峰值");
      attributeList.add("加速段电流脉冲指标");
      attributeList.add("加速段最大跟随误差比");
      attributeList.add("减速时间");
      attributeList.add("减速段电流峰峰值");
      attributeList.add("减速段电流脉冲指标");
      attributeList.add("减速段最大跟随误差比");
      attributeList.add("恒速段最大跟随误差比");
      attributeList.add("恒速段电流均方根");
      attributeList.add("恒速段电流均方根幅值");
      attributeList.add("恒速段电流方差");
      attributeList.add("恒速段电流歪度");
      attributeList.add("恒速段电流峭度");
      attributeList.add("恒速段电流波形指标");
      attributeList.add("恒速段电流裕度指标");
      Map map = new LinkedHashMap();
      SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT+0"));
      Date date = new Date(product.getUploadTime() * 1000L);
      map.put("记录上传时间", simpleDateFormat.format(date));
      map.put("电机数量", product.getMacAxNum());
      if (product.getPartCheckFlag() == 0) {
         map.put("是否成套检测", "成套");
      } else {
         map.put("是否成套检测", "散件");
      }

      if (product.getPassFlag() == 1) {
         map.put("检测是否合格", "合格");
      } else {
         map.put("检测是否合格", "不合格");
      }

      SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
      map.put("成套系统出厂日期", sf.format(product.getMacProductTime()));
      map.put("客户名", product.getCustomerName());
      map.put("合同号", product.getContractCode());
      map.put("型号规格", product.getMacModel());
      map.put("出厂编号", product.getMacNO());
      map.put("系统SN号", product.getMacSN());
      map.put("生产时间", product.getSysProductTime());
      map.put("IPC编号", product.getIpcCode());
      map.put("总线式I/O单元型号", product.getIoUnitType());
      map.put("总线式I/O单元编号", product.getIoUnitId());
      map.put("系统软件版本标准值", ncVer.getSoftVer());
      map.put("系统软件版本检测值", ncVer.getTestSoftVer());
      map.put("Ncu版本标准值", ncVer.getNcuVer());
      map.put("Ncu版本检测值", ncVer.getTestNcuVer());
      map.put("PLC内核版本标准值", ncVer.getPlcVer());
      map.put("PLC内核版本检测值", ncVer.getTestPlcVer());
      map.put("DRV驱动程序版本标准值", ncVer.getDrvVer());
      map.put("DRV驱动程序版本检测值", ncVer.getTestDrvVer());
      map.put("参数标准值", ncVer.getUserParmVer());
      map.put("参数检测值", ncVer.getTestUserParmVer());
      map.put("用户PLC标准值", ncVer.getUserPlcVer());
      map.put("用户PLC检测值", ncVer.getTestUserPlcVer());
      map.put("OS标准值", ncVer.getOsVer());
      map.put("OS检测值", ncVer.getTestOsVer());
      map.put("Fpga标准值", ncVer.getFpgaVer());
      map.put("Fpga检测值", ncVer.getTestFpgaVer());
      mapName.put("accTime", "加速时间");
      mapName.put("accElecFF", "加速段电流峰峰值");
      mapName.put("accElecPulse", "加速段电流脉冲指标");
      mapName.put("accMaxFollowRate", "加速段最大跟随误差比");
      mapName.put("decTime", "减速时间");
      mapName.put("decElecFF", "减速段电流峰峰值");
      mapName.put("decElecPulse", "减速段电流脉冲指标");
      mapName.put("decMaxFollowRate", "减速段最大跟随误差比");
      mapName.put("constMaxFollowRate", "恒速段最大跟随误差比");
      mapName.put("constElecRMS", "恒速段电流均方根");
      mapName.put("constElecRMSMag", "恒速段电流均方根幅值");
      mapName.put("constElecVariance", "恒速段电流方差");
      mapName.put("constElecSkewness", "恒速段电流歪度");
      mapName.put("constElecKurtosis", "恒速段电流峭度");
      mapName.put("constElecWaveQuota", "恒速段电流波形指标");
      mapName.put("constElecMarginQuota", "恒速段电流裕度指标");

      for(int j = 0; j < 3; ++j) {
         map.put("轴" + j + "伺服型号标准值", "");
         map.put("轴" + j + "伺服型号检测值", "");
         map.put("轴" + j + "伺服版本标准值", "");
         map.put("轴" + j + "伺服版本检测值", "");
         map.put("轴" + j + "加速时间", "");
         map.put("轴" + j + "加速段电流峰峰值", "");
         map.put("轴" + j + "加速段电流脉冲指标", "");
         map.put("轴" + j + "加速段最大跟随误差比", "");
         map.put("轴" + j + "减速时间", "");
         map.put("轴" + j + "减速段电流峰峰值", "");
         map.put("轴" + j + "减速段电流脉冲指标", "");
         map.put("轴" + j + "减速段最大跟随误差比", "");
         map.put("轴" + j + "恒速段最大跟随误差比", "");
         map.put("轴" + j + "恒速段电流均方根", "");
         map.put("轴" + j + "恒速段电流均方根幅值", "");
         map.put("轴" + j + "恒速段电流方差", "");
         map.put("轴" + j + "恒速段电流歪度", "");
         map.put("轴" + j + "恒速段电流峭度", "");
         map.put("轴" + j + "恒速段电流波形指标", "");
         map.put("轴" + j + "恒速段电流裕度指标", "");
      }

      ObjectMapper mapper = new ObjectMapper();
      Iterator var15 = axisList.iterator();

      HashMap infoResult;
      HashMap allResult;
      while(var15.hasNext()) {
         Axis axis = (Axis)var15.next();
         List axisMapList = new ArrayList();
         infoResult = new HashMap();
         Map axisMap2 = new HashMap();
         Map axisMap3 = new HashMap();
         allResult = new HashMap();
         Map axisMap5 = new HashMap();
         infoResult.put("name", axis.getAxisName() + "电机编号");
         infoResult.put("value", axis.getElecId());
         axisMapList.add(infoResult);
         axisMap2.put("name", axis.getAxisName() + "电机型号");
         axisMap2.put("value", axis.getElecModel());
         axisMapList.add(axisMap2);
         axisMap3.put("name", axis.getAxisName() + "伺服驱动编号");
         axisMap3.put("value", axis.getServoId());
         axisMapList.add(axisMap3);
         allResult.put("name", axis.getAxisName() + "伺服驱动型号");
         allResult.put("value", axis.getServoModel());
         axisMapList.add(allResult);
         axisMap5.put("name", axis.getAxisName() + "伺服生产时间");
         axisMap5.put("value", axis.getServoProductTime());
         axisMapList.add(axisMap5);
         map.put(axis.getAxisName(), mapper.writeValueAsString(axisMapList));
      }

      var15 = servos.iterator();

      while(true) {
         Servo servo;
         do {
            if (!var15.hasNext()) {
               var15 = motorChecks.iterator();

               while(var15.hasNext()) {
                  MotorCheck motorCheck = (MotorCheck)var15.next();
                  map.put(motorCheck.getAxisName() + mapName.get(motorCheck.getElecName()), motorCheck.getCur());
               }

               map.put("标识", "88.222.1/10000099.0." + product.getMacNO() + ".2021");
               map.put("制造商", "武汉华中数控股份有限公司");
               map.put("供应商", "武汉华中数控股份有限公司");
               map.put("数量", "1");
               map.put("单位", "套");
               map.put("电话", "027-87180025");
               map.put("参数信息", "88.222.1/10000099.info." + product.getMacNO() + ".2021");
               map.put("溯源信息", "88.222.1/10000099.source." + product.getMacNO() + ".2021");
               List mainList = new ArrayList();
               List infoList = new ArrayList();
               Map mainResult = new HashMap();
               infoResult = new HashMap();
               int mainCount = 0;
               int infoCount = 0;

               HashMap value;
               HashMap data;
               String v;
               Iterator var34;
               Map.Entry entry;
               for(var34 = mainMap.entrySet().iterator(); var34.hasNext(); ++mainCount) {
                  entry = (Map.Entry)var34.next();
                  value = new HashMap();
                  data = new HashMap();
                  data.put("format", "string");
                  v = map.get(entry.getKey()) == null ? " " : map.get(entry.getKey()).toString();
                  data.put("value", v);
                  value.put("data", data);
                  value.put("auth", "");
                  value.put("index", 2000 + mainCount);
                  value.put("type", entry.getValue());
                  mainList.add(value);
               }

               for(var34 = infoMap.entrySet().iterator(); var34.hasNext(); ++infoCount) {
                  entry = (Map.Entry)var34.next();
                  value = new HashMap();
                  data = new HashMap();
                  data.put("format", "string");
                  v = map.get(entry.getKey()) == null ? " " : map.get(entry.getKey()).toString();
                  data.put("value", v);
                  value.put("data", data);
                  value.put("auth", "");
                  value.put("index", 2000 + infoCount);
                  value.put("type", entry.getValue());
                  infoList.add(value);
               }

               mainResult.put("handle", "88.222.1/10000099.0." + product.getMacNO() + ".2021");
               mainResult.put("templateVersion", "华中数控系统零部件成品及半成品");
               mainResult.put("value", mainList);
               infoResult.put("handle", "88.222.1/10000099.info." + product.getMacNO() + ".2021");
               infoResult.put("templateVersion", "华中数控成套系统参数信息");
               infoResult.put("value", infoList);
               allResult = new HashMap();
               allResult.put("main", mainResult);
               allResult.put("info", infoResult);
               return allResult;
            }

            servo = (Servo)var15.next();
         } while(!"轴0".equals(servo.getAxisName()) && !"轴1".equals(servo.getAxisName()) && !"轴2".equals(servo.getAxisName()));

         map.put(servo.getAxisName() + "伺服型号标准值", servo.getServoType());
         map.put(servo.getAxisName() + "伺服型号检测值", servo.getTestServoType());
         map.put(servo.getAxisName() + "伺服版本标准值", servo.getServoVer());
         map.put(servo.getAxisName() + "伺服版本检测值", servo.getTestServoVer());
      }
   }
}
